<template>
  <div class="index-container">
    <el-form :inline="true" >
      <el-form-item label="学校名">
        <el-input v-model="queryForm.name" placeholder="学校名" />
      </el-form-item>
      <el-form-item label="学校状态">
        <el-select v-model="queryForm.status" placeholder="请选择">
          <el-option
            v-for="item in schoolStatusData"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-search"
          type="primary" @click="fetchData">
          查询
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="tableSort"
      :data="list"
    >
      <el-table-column show-overflow-tooltip label="学校名" prop="name"/>
       <el-table-column show-overflow-tooltip label="校区地址" prop="location"/>
      <el-table-column show-overflow-tooltip label="所属用户" prop="adminId"/>
      <el-table-column show-overflow-tooltip label="校区开放的服务" prop="service">
        <template #default="{ row }">
          {{row.service}}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="学校状态" prop="status" :formatter="formatstatus" />
      <el-table-column show-overflow-tooltip label="当前状态的原因" prop="reason"/>
      <el-table-column show-overflow-tooltip label="校园公告" prop="notice" />
      <el-table-column label="创建时间" prop="createTime" :formatter="formatTime"/>
      <el-table-column label="操作" width="220px">
        <template #default="{ row }">
          <el-button type="text" v-if="row.status == 1" @click="auditClick(row)">学校审核</el-button>
          <el-button type="text" @click="operaClick(row)">
            {{row.status == 2 ?'下架':'上架'}}
          </el-button>
          <el-button type="text" @click="editClick(row)">学校修改</el-button>
          <el-button type="text" @click="noticeClick(row)">公告修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.currentPage"
      layout="total, sizes, prev, pager, next, jumper"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <Audit ref="CreateAudit" @onOk="fetchData"></Audit>
    <Form ref="CreateForm" @onOk="fetchData"></Form>
    <Notice ref="CreateNotice" @onOk="fetchData"></Notice>

  </div>
</template>

<script>
import {
  Vue,
  ref,
  toRefs,
  shallowRef,
  reactive,
  watch,
  watchEffect,
  getCurrentInstance,
  computed,
  defineComponent,
  nextTick,
  inject,
  onMounted,
  onUnmounted,
} from 'vue'
import { Message,MessageBox } from 'element-ui'
import Audit from '@/components/VabSchool/audit'
import Form from '@/components/VabSchool/form'
import Notice from '@/components/VabSchool/notice'
import store from '@/store'
import {getSchoolPage,setSchoolOnOffLine} from '@/api/school/school_api.js'
export default {
  name: 'SchoolList',
  components: {
    Audit,
    Form,
    Notice
  },
  setup(props, { emit }) {
    const CreateAudit = ref(null)
    const CreateForm = ref(null)
    const CreateNotice = ref(null)
    const Interface = reactive({
      handleSizeChange(val) {
        query.queryForm.pageSize = val
        Interface.fetchData()
      },
      handleCurrentChange(val) {
        query.queryForm.currentPage = val
        Interface.fetchData()
      },
      async fetchData(){
        const {data} = await getSchoolPage(query.queryForm)
        if(data && Array.isArray(data.schoolList)){
          query.list = data.schoolList
          query.total = data.total
        }
      },
      formatstatus(row, column){
        const statusMap = {
          1: '待审核',
          2: '正常',
          3: '下线'
        }
        return statusMap[row.status]
      },
      formatTime(row, column){
        let data = row[column.property]
        let dtime = new Date(data)
        const year = dtime.getFullYear()
        let month = dtime.getMonth() + 1
        if (month < 10) {
          month = '0' + month
        }
        let day = dtime.getDate()
        if (day < 10) {
          day = '0' + day
        }
        return year+ '-' + month+ '-' + day
      },
      auditClick(row){
        CreateAudit.value.auditClick(row)
      },
      editClick(row){
        CreateForm.value.formClick(row)
      },
      noticeClick(row){
        CreateNotice.value.formClick(row)
      },
      operaClick(row){
        let txt = row.status == 2 ?'下架后学校对小程序用户不可见，是否继续？':'上架后学校对小程序用户可见，是否继续?'
        MessageBox.prompt('请输入理由', txt, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /\s*\S+?/,
          inputErrorMessage: '请输入理由'
        }).then(async ({ value }) => {
          let params = {}
          params.id = row.id
          params.operaType = row.status == 2 ? 2:1
          params.reason = value
          const {code} =await setSchoolOnOffLine(params)
          if(code == 0){
             Message.success('操作成功');
          Interface.fetchData()
          }
        }).catch(() => {});
      }
    })
    const query = reactive({
      schoolStatusData: computed(() => store.getters['baseStore/schoolStatusData']),
      list:[],
      total:0,
      queryForm: {
        pageSize:10,
        currentPage:1,
        status:'',
        name:'',
      }
    })
    onMounted(() => {
      Interface.fetchData() //初始化表格数据
    })
    return {
      CreateNotice,
      CreateAudit,
      CreateForm,
      ...toRefs(query),
      ...toRefs(Interface)
    }
  }
} 
</script> 
 